package com.panopset.io;

import java.io.File;

import com.panopset.compat.Strings;

/**
 * Recursively traverses a Directory tree, and processes any found Files.
 * @author Karl Dinwiddie
 *
 */
public final class Directory {

    /**
     * Processor.
     */
    private final Processor p;

    /**
     * @param file File or directory to process.
     */
    private void process(final File file) {
        if (file.isDirectory()) {
            for (File f : file.listFiles()) {
                process(f);
            }
        } else {
            p.process(file);
        }
    }

    /**
     * @param args File and/or Directory paths.
     */
    public void process(final String... args) {
        if (args == null) {
            return;
        }
        for (String s : args) {
            if (!Strings.isPopulated(s)) {
                continue;
            }
            File f = new File(s);
            if (!f.exists()) {
                continue;
            }
            process(f);
        }
    }

    /**
     * @param processor File processor.
     */
    public Directory(final Processor processor) {
        p = processor;
    }

    /**
     * File processor.
     */
    public interface Processor {

        /**
         * @param file File to process.
         */
        void process(final File file);
    }
}
